<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品编号" prop="commName">
      <el-input v-model="dataForm.id" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="commodityName">
      <el-input v-model="dataForm.commName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品类型" prop="commodityType">
      <el-input v-model="dataForm.commodityType" placeholder="商品类型"></el-input>
    </el-form-item>
    <el-form-item label="品牌名称" prop="brand">
      <el-input v-model="dataForm.commInfo" placeholder="品牌名称"></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="likeCount">
      <el-input v-model="dataForm.likeCount" placeholder="点赞数"></el-input>
    </el-form-item>
    <el-form-item label="收藏数" prop="collectCount">
      <el-input v-model="dataForm.collectCount" placeholder="收藏数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          commName: '',
          commUrlId: '',
          brandName: '',
          commInfo: '',
          commodityType: '',
          likeCount: '',
          collectCount: '',
          status: ''
        },
        dataRule: {
          commName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          commUrlId: [
            { required: true, message: '商品图片关联id不能为空', trigger: 'blur' }
          ],
          brandName: [
            { required: true, message: '品牌名称不能为空', trigger: 'blur' }
          ],
          commInfo: [
            { required: true, message: '产品简介不能为空', trigger: 'blur' }
          ],
          likeCount: [
            { required: true, message: '点赞数不能为空', trigger: 'blur' }
          ],
          collectCount: [
            { required: true, message: '收藏数不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/commodity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commName = data.commodity.commodityName
                this.dataForm.commUrlId = data.commodity.id
                this.dataForm.brandName = data.commodity.brand
                this.dataForm.commInfo = data.commodity.commodityInfo
                this.dataForm.commodityType = data.commodity.commodityType
                this.dataForm.likeCount = data.commodity.likeCount
                this.dataForm.collectCount = data.commodity.collectCount
                this.dataForm.status = data.commodity.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/commodity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'commName': this.dataForm.commName,
                'commUrlId': this.dataForm.commUrlId,
                'brandName': this.dataForm.brandName,
                'commInfo': this.dataForm.commInfo,
                'likeCount': this.dataForm.likeCount,
                'collectCount': this.dataForm.collectCount,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
